package cn.lil.servlet;

import cn.lil.dao.OrderDao;
import cn.lil.model.Order;
import cn.lil.model.User;
import cn.lil.service.OrderService;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "OrderListServlet", value = "/OrderListServlet")
public class OrderListServlet extends HttpServlet {
    private OrderService orderService = new OrderService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Order> order = null;
        User u = (User) request.getSession().getAttribute("user");
        if (u == null) {
            response.sendRedirect("/index.jsp");
            return;
        }
        try {
            order = orderService.selectAll(u.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.getSession().setAttribute("order", order.get(0));
        System.out.println(order);
        request.getRequestDispatcher("/OrderItemListServlet").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Order> order = null;
        User u = (User) request.getSession().getAttribute("user");
        if (u == null) {
            response.sendRedirect("/index.jsp");
            return;
        }
        try {
            order = orderService.selectAll(u.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.getSession().setAttribute("order", order.get(0));
        System.out.println(order);
        request.getRequestDispatcher("/OrderItemListServlet").forward(request, response);
    }
}
